//
//  MineHomeViewController.swift
//  GhostWorld
//
//  Created by Kam on 2018/5/14.
//  Copyright © 2018 LKGood. All rights reserved.
//

import UIKit

class MineHomeViewController: BaseViewController {
  
  // MARK: - IBOutlets
  
  @IBOutlet weak var topViewTopConstraint: NSLayoutConstraint!
  
  // MARK: - Private vars
  
  
  
  // MARK: - Private methods

  private func performSegueForUser(withIdentifier identifier: String) {
    if identifier == "ShowPlaylistSegue" ||
      identifier == "ShowFavSegue" ||
      identifier == "ShowPurchasedSegue" {
      
      if UserManager.shared.currentUser != nil {
        performSegue(withIdentifier: identifier, sender: nil)
      } else {
        let alert = UIAlertController (
          title: "尚未登录",
          message: "请登录后再试",
          cancelTitle: "取消",
          cancelHandler: nil,
          confirmTitle: "登录") { _ in
            let loginViewController = UIStoryboard(name: "Account", bundle: nil).instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
            loginViewController.addCloseButton()
            let nav = BaseNavigationController(rootViewController: loginViewController, dismissDelegate: self)
            nav.isPresentedModally = true
            self.present(nav, animated: true)
        }
        present(alert, animated: true)
      }
    }
  }
  
  // MARK: - Public vars
  
  
  
  // MARK: - Lifecycle
  
  override func viewDidLoad() {
    super.viewDidLoad()
    
    
    prefersLargeTitles = true
  }
  
  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }
  
  // MARK: - IBActions
  
  @IBAction func showPlaylists() {
    performSegueForUser(withIdentifier: "ShowPlaylistSegue")
  }
  
  @IBAction func showFavs() {
    performSegueForUser(withIdentifier: "ShowFavSegue")
  }
  
  @IBAction func showPurchased() {
    performSegueForUser(withIdentifier: "ShowPurchasedSegue")
  }
  
  @IBAction func showPlayHistory(_ sender: UIBarButtonItem) {
    if let playHistoryNavigationController = UIStoryboard(name: "Player", bundle: nil).instantiateViewController(withIdentifier: "PlayHistoryNavigationController") as? BaseNavigationController {
      playHistoryNavigationController.dismissDelegate = self
      present(playHistoryNavigationController, animated: true)
    }
  }
  
  // MARK: - Public methods
  
}

// MARK: - BaseNavigationControllerDismissDelegate

extension MineHomeViewController: BaseNavigationControllerDismissDelegate {
  func dismiss(navigationController: BaseNavigationController, completion: (() -> Void)?) {
    dismiss(animated: true, completion: completion)
  }
}
